/*
 * @Description: 
 * @Author: 赵大壮
 * @Date: 2021-12-06 17:18:37
 * @LastEditTime: 2021-12-06 17:36:29
 * @LastEditors: 赵大壮
 */
import { Component, Vue, Inject, Watch } from "vue-property-decorator";
import DZServiceQuestion from "../../service/DZServiceQuestion";
import UiDialogAddQuestion from "@/components/ui-dialog/UiDialogAddQuestion.vue"
import UiDialogAddAnswer from "@/components/ui-dialog/UiDialogAddAnswer.vue"
import DZModelQuestion from "../../model/DZModelQuestion";
import DZModelAnswer from "../../model/DZModelAnswer";

@Component({
    components: { UiDialogAddQuestion, UiDialogAddAnswer }
})
export default class Index extends Vue {

    s_question = new DZServiceQuestion();

    dialog_add_question = false;

    dialog_add_answer = false;

    option = 1;

    first_init = true;

    created() { }

    mounted() {
        this.first_init = true;
        this.s_question.assess = this.$route.query.id as string;
        this.option = parseInt(this.$route.query.option as string);
        this.action_list_question();
    }

    beforeDestroy() {

    }

    action_go_back() {
        this.$router.push({ path: this.$route.query.father as string })
    }

    action_list_question() {
        this.s_question.net_list_question();
    }

    action_edit_question(item) {
        this.s_question.m_question = JSON.parse(JSON.stringify(item));
        this.dialog_add_question = true;
    }

    @Watch('s_question.list_question')
    actionWatchQuestion(vallue, oldValue) {
        this.$nextTick(() => {
            if (this.first_init) {
                return;
            }
            var div = document.getElementById('questionList');
            div.scrollTop = div.scrollHeight;
        })
    }

    action_add_question(data) {
        this.dialog_add_question = false;
        this.s_question.m_question = JSON.parse(JSON.stringify(data));
        if (data.id) {
            this.s_question.net_edit_question(() => {
                this.s_question.m_question = new DZModelQuestion();
                this.action_list_question();
            });
        } else {
            this.s_question.net_add_question(() => {
                this.first_init = false;
                this.s_question.m_question = new DZModelQuestion();
                this.action_list_question();
            });
        }
    }

    action_list_anser() {
        this.s_question.net_list_answer();
    }

    action_question_item_click(item) {
        this.s_question.select_item_id = item.id;
        this.action_list_anser();
    }


    action_add_answer(data) {
        this.dialog_add_answer = false;
        this.s_question.m_answer = JSON.parse(JSON.stringify(data));
        if (data.id) {
            this.s_question.net_edit_answer(() => {
                this.s_question.m_answer = new DZModelAnswer();
                this.action_list_anser();
            });
        } else {
            this.s_question.net_add_answer(() => {
                this.first_init = false;
                this.s_question.m_answer = new DZModelAnswer();
                this.action_list_anser();
            });
        }
    }

    action_edit_answer(item) {
        this.s_question.m_answer = JSON.parse(JSON.stringify(item));
        this.dialog_add_answer = true;
    }
}